// components/calendar/index.js

var app = getApp();
Component({
    /**
     * 组件的属性列表
     */  
    /**
     * 组件的初始数据
     */
    data: {
      weekList: ['一', '二', '三', '四', '五', '六', '日'], // 周列表
      monthDayList: [], // 当月的日列表
      currentYear: '', // 当前年
      currentMonth: '', // 当前月
      currentDay: '', // 当前日
      today: '', // 今日
      selectedDate: '',
      signedList: [], // 将签到日期格式化
      isDisabledNextMonth: true, // 是否能跳转下一个月
      sign_status:'',
      todayStyle:'is-today',
      nowTime:new Date().getTime()
    },
  
    observers: {
      currentMonth() {
        const today = new Date()
        const todayYear = today.getFullYear()
        const todayMonth = today.getMonth() + 1
        if (this.data.currentYear > todayYear || (this.data.currentYear == todayYear && this.data.currentMonth >= todayMonth)) {
          this.setData({
            isDisabledNextMonth: true
          })
        } else {
          this.setData({
            isDisabledNextMonth: false
          })
        }
      }
    },
    lifetimes: {
      // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
      created: function () {
        // 组件实例刚刚被创建时
        this.getSign()
      },
      ready: function () {
        // 在组件布局完成后执行
        this.initCurrentDate()
        this.getSign()
      },
      
      moved: function () {
        // 在组件实例被移动到节点树另一个位置时执行
      },
      detached: function () {
        // 组件实例被从页面节点树移除时
      },
    },
  
    /**
     * 组件的方法列表
     */
    methods: {
      // 格式化为兼容ios的格式
      formatIosDate(date) {
        return date.replace(/\-/g, '/')
      },
      // 格式化时间
      formatTime(date) {
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
  
        return [year, month, day].join('/')
      },
      // 十位数补0
      formatZero(number) {
        return number
      },
      // 初始化当前时间
      initCurrentDate(date) {
        let currentDate = date || new Date()
        let currentYear = currentDate.getFullYear()
        let currentMonth = currentDate.getMonth() + 1
        let currentDay = currentDate.getDate()
        // 传的日期为空默认今日
        if (!date) {
          const today = `${currentYear}/${currentMonth}/${currentDay}`
          this.setData({
            today: today
          })
        }
  
        this.setData({
          currentYear: currentYear,
          currentMonth: currentMonth,
          currentDay: currentDay
        }, () => {
          this.initMonthDayList()
        })
      },
      // 初始化当前月的日期列表
      initMonthDayList() {
        // 获取当月1号是周几
        let date = new Date(this.data.currentYear, this.data.currentMonth-1, 1)
        let weekDay = date.getDay() 
  
        // 获取当月的总天数
        let monthFirstDay = new Date(this.data.currentYear, this.data.currentMonth, 0)
        let monthDay = monthFirstDay.getDate()
  
        let list = []
        if(weekDay == 0){
          for (let index = 0; index < 6; index++) {
            list.push(null)
          }
        }else{
          for (let index = 0; index < weekDay - 1; index++) {
            list.push(null)
          }
        }
        for (let index = 1; index <= monthDay; index++) {
          if(this.data.currentMonth >=1 && this.data.currentMonth<10){
            if(index>=1 && index <10 ){
              const itemDate = `${this.data.currentYear}/0${this.data.currentMonth}/0${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
              };
              list.push(item);
            }else{
              const itemDate = `${this.data.currentYear}/0${this.data.currentMonth}/${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
              };
              list.push(item);
            }
          }
          else if(this.data.currentMonth >=10){
            if(index>=1 && index <10 ){
              const itemDate = `${this.data.currentYear}/${this.data.currentMonth}/0${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
              };
              list.push(item);
            }else{
              const itemDate = `${this.data.currentYear}/${this.data.currentMonth}/${index}`;
              let item = {
                day: index,
                date: itemDate, // 具体日期
                time: new Date(`${this.data.currentYear}/${this.data.currentMonth}/${index}`).getTime()
              };
              list.push(item);
            }
                   if(this.currentMonth >=1 && this.currentMonth<10){
          if(index>=1 && index <10 ){
            const itemDate = `${this.currentYear}/0${this.currentMonth}/0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }else{
            const itemDate = `${this.currentYear}/0${this.currentMonth}/${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }
        }
        else if(this.currentMonth >=10){
          if(index>=1 && index <10 ){
            const itemDate = `${this.currentYear}/${this.currentMonth}/0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }else{
            const itemDate = `${this.currentYear}/${this.currentMonth}/${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }

        }
          }
        }
        this.setData({
          monthDayList: list
        })
      },
      // 选中当前日期
      handleSelectDate(event) {
        const item = event.currentTarget.dataset.item
        if (item) {
          this.setData({
            selectedDate: item.date
          })
        }
      },
      // 上一月
      handlePreMonth() {
        this.handleToggleMonth(-1)
        this.getnewDate()
      },
      // 下一月
      handleNextMonth() {
        if (!this.data.isDisabledNextMonth) {
          this.handleToggleMonth(1)
          this.getnewDate()
        }
      },
      // 切换月
      handleToggleMonth(data) {
        let month = this.data.currentMonth += data
        let year = this.data.currentYear
        if (month <= 0) {
          month = 12
          year--
        } else if (month > 12) {
          month = 1
          year++
        }
        this.setData({
          currentYear: year,
          currentMonth: month
        }, () => {
          this.initMonthDayList()
          this.triggerEvent('change', { date: `${year}/${month}` })
        })
      },
      getnewDate(){
        var that = this;
        let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
        app._getNetWork({
          data:{ month: this.data.currentMonth, year: this.data.currentYear},
          url: urlStr,
          success:function (res){
            if (res.data.result == 1) {

              that.setData({
                signedList: res.data.data.sign_log,

              })
            }
          }
        })
      },
      getSign() {
        var that = this;
        let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
        app._getNetWork({
          url: urlStr,
          success: function (res) {
            if (res.data.result == 1) {
              wx.hideLoading();
              wx.setStorage({
                key: 'sign_log',
                data: res.data.data.sign_log
              });
              that.setData({
                sign_status: res.data.data.sign_status,
                signedList: res.data.data.sign_log,
              });

              wx.setStorage({
                key: 'cumulative',
                data: res.data.data.cumulative
              });
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              });
            }
          }
        });
      },
    }
  })
  
  